package scoreAnalyze;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;

/**
 * 自定义StudentWritable数据类型，包含学号和姓名两个字段
 * 因StudentWritable作为key输出，实现了CompareTo比较功能
 * 实现了序列化、反序列化
 */
public class StudentWritable implements WritableComparable<StudentWritable>{
    String id, name;
    public StudentWritable(){
        super();
    }
    public StudentWritable(String id, String name){
        super();
        this.id = id;
        this.name = name;
    }
    public void write(DataOutput out) throws IOException{
        //序列化
        out.writeUTF(id);
        out.writeUTF(name);
    }
    public void readFields(DataInput in) throws IOException{
        //反序列化
        this.id = in.readUTF();
        this.name = in.readUTF();
    }
    public String toString(){
        return this.id + "\t" + this.name;
    }
    public int compareTo(StudentWritable s){
        if(id.compareTo(s.id) > 0)
            return 1;
        else if(id.compareTo(s.id) < 0)
            return -1;
        else
            return 0;
    }
}
